/*
 * @Author: 彭林
 * @Date: 2021-06-29 15:16:13
 * @LastEditTime: 2021-07-01 15:40:10
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/service/student_card.go
 */
package service

import (
	"fmt"
	"saas-app/app/dao"
	"saas-app/app/model"

	"github.com/gogf/gf/frame/g"
)

type StudentCardRepository interface {
	GetStudentCardList(school_id, campus_id, status, student_id int) g.List
	CreateStudentCard(studentCard *model.StudentCard) bool
}

type studentCard struct{}

func StudentCardService() StudentCardRepository {
	return &studentCard{}
}

func (s studentCard) GetStudentCardList(school_id, campus_id, status, student_id int) g.List {
	db := g.Table("student_card")
	if status >= 0 {
		fmt.Println("status 1: ", status)
		db = db.Where("student_card.status = ?", status)
	}
	db = db.Fields("student_card.*, card.card_name as card_name, card.card_type").LeftJoin("card", "student_card.card_type_id = card.id").Where("student_card.student_id = ?", student_id)
	r, err := db.All()
	if err != nil {
		panic(err)
	}
	return r.List()
}

func (s studentCard) CreateStudentCard(studentCard *model.StudentCard) bool {

	if studentCard.SchoolId <= 0 {
		panic("参数错误")
	}

	if studentCard.CampusId <= 0 {
		panic("参数错误")
	}

	_, err := dao.StudentCard.Save(studentCard)

	if err != nil {
		panic(err)
	}

	return true
}
